﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class BillShipmentRepaymentService
    {
        /// <summary>
        /// 查询客户还款信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BillShipmentRepayment GetBillShipmentRepayment(long id)
        {
            BillShipmentRepayment repayment = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    repayment = new BillShipmentRepayment();
                    repayment.Id = id;
                    repayment = new BillShipmentRepaymentDAO().GetBillShipmentRepayment(conn, repayment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return repayment;
        }

        /// <summary>
        /// 查询客户还款信息列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo"></param>
        /// <param name="shipmentno"></param>
        /// <param name="memberno"></param>
        /// <param name="adminUsername"></param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public DataTable GetBillShipmentRepaymentTable(int pageindex, int pagesize, String enterpriseNo, String shipmentno, String memberno,
            String adminUsername, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillShipmentRepayment repayment = new BillShipmentRepayment();
                    repayment.Pageindex = pageindex;
                    repayment.Pagesize = pagesize;
                    repayment.EnterpriseNo = enterpriseNo;
                    repayment.ShipmentNo = shipmentno;
                    repayment.MemberNo = memberno;
                    repayment.AdminUsername = adminUsername;
                    repayment.Ascfield = ascfield;
                    repayment.Descfield = descfield;
                    dt = new BillShipmentRepaymentDAO().GetBillShipmentRepaymentTable(conn, repayment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询客户还款信息记录数
        /// </summary>
        /// <param name="enterpriseNo"></param>
        /// <param name="shipmentno"></param>
        /// <param name="memberno"></param>
        /// <param name="adminUsername"></param>
        /// <returns></returns>
        public long GetBillShipmentRepaymentRecord(String enterpriseNo, String shipmentno, String memberno, String adminUsername)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillShipmentRepayment repayment = new BillShipmentRepayment();
                    repayment.EnterpriseNo = enterpriseNo;
                    repayment.ShipmentNo = shipmentno;
                    repayment.MemberNo = memberno;
                    repayment.AdminUsername = adminUsername;
                    record = new BillShipmentRepaymentDAO().GetBillShipmentRepaymentRecord(conn, repayment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 新增客户还款信息
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="repayment"></param>
        /// <returns></returns>
        public long InsertBillShipmentRepayment(DbConnection conn, BillShipmentRepayment repayment)
        {
            long id = 0;
            try
            {
                id = new BillShipmentRepaymentDAO().InsertBillShipmentRepayment(conn, repayment);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 新增客户还款信息
        /// </summary>
        /// <param name="repayment"></param>
        /// <returns></returns>
        public long InsertBillShipmentRepayment(BillShipmentRepayment repayment)
        {
            long id = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    conn.Open();
                    trans = conn.BeginTransaction();
                    id = new BillShipmentRepaymentDAO().InsertBillShipmentRepayment(conn, repayment);
                    if (id > 0)
                    {
                        int flag = new BillShipmentService().UpdateDebtAmount(conn, repayment.ShipmentNo, repayment.RemainAmount);
                        if (flag > 0)
                            trans.Commit();
                        else
                            throw new Exception("update billShipment debt error!");
                    }
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    trans = null;
                }
            }
            return id;
        }

        /// <summary>
        /// 更新用户还款信息
        /// </summary>
        /// <param name="repayment"></param>
        /// <returns></returns>
        public int UpdateBillShipmentRepayment(BillShipmentRepayment repayment)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new BillShipmentRepaymentDAO().UpdateBillShipmentRepayment(conn, repayment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除用户还款信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteBillShipmentRepayment(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        BillShipmentRepaymentDAO repaymentDAO = new BillShipmentRepaymentDAO();
                        BillShipmentRepayment repayment = new BillShipmentRepayment();
                        foreach (String lopid in arr)
                        {
                            repayment.Id = Int64.Parse(lopid);
                            count += repaymentDAO.DeleteBillShipmentRepayment(conn, repayment);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
